<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/15
 * Time: 11:17 AM
 */

namespace App\Library\KdNiao;


use App\Library\KdNiao\Request\KdPrintOrderRequest;
use App\Library\KdNiao\Request\KdQueryRequest;
use App\Library\KdNiao\Response\KdQueryResponse;

class KdClient
{
    public $EBusinessID = '1396022';
    public $AppKey = 'e324bd29-150b-4ed8-bfe6-4a3e04dce87e';
    public $DataType = '2';

    /**
     * Json方式 查询订单物流轨迹
     */
    public function sendQueryRequest(KdQueryRequest $request){
        //$requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";

        $url = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
        $requestData = json_encode($request->getParams());
        $datas = array(
            'EBusinessID' => $this->EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => $this->DataType,
        );
        $datas['DataSign'] = $this->encrypt($requestData, $this->AppKey);
        $result = $this->curlPost($url, $datas);

        return new KdQueryResponse(json_decode($result, true));
    }

    /**
     * 打印面单
     * @param KdPrintOrderRequest $request
     * @return string
     */
    public function sendPrintOrderRequest(KdPrintOrderRequest $request)
    {
        $url = 'http://www.kdniao.com/External/PrintOrder.aspx';
        $request_data = json_encode($request->getParams());
        $request_data_encode = urlencode($request_data);
        $data_sign = $this->encrypt(request()->getClientIp().$request_data_encode, $this->AppKey);
        //是否预览，0-不预览 1-预览
        $is_priview = '0';

        //组装表单
        $form = '<form id="form1" method="POST" action="'.$url.'"><input type="text" name="RequestData" value="'.$request_data.'"/><input type="text" name="EBusinessID" value="'.$this->EBusinessID.'"/><input type="text" name="DataSign" value="'.$data_sign.'"/><input type="text" name="IsPriview" value="'.$is_priview.'"/></form><script>form1.submit();</script>';
        return $form;
    }

    /**
     * @param $url
     * @param string $data
     * @param int $ssl
     * @param int $timeout
     * @return mixed
     */
    private function curlPost($url, $data='', $ssl=0, $timeout=500){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $ssl);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $ssl);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, ['application/x-www-form-urlencoded','charset=utf-8']);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    /**
     * 电商Sign签名生成
     * @param string data 内容
     * @param string appkey Appkey
     * @return string DataSign签名
     */
    private function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }
}
